package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.CreativeProductAdapter;
import com.dinadi.yidi.adapter.ProductSubAllAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.CreativeProductBean;
import com.dinadi.yidi.bean.CreativeProductRequest;
import com.dinadi.yidi.bean.CreativeProductRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.EmptyUtils;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24   商店专题全部
 * Time: 11:02
 */

public class ProductSubjectAllActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, tv_title, tv_desc;
    private LinearLayout layout_head;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private ProductSubAllAdapter subAdapter;
    private List<CreativeProductRequestEntity> dataList = new ArrayList<>();
    private int page = 1;
    private String token, proSubId, userId, type, title;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(ProductSubjectAllActivity.this).inflate(R.layout.activity_product_sub_all, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_desc = (TextView) view.findViewById(R.id.tv_desc);
        layout_head = (LinearLayout) view.findViewById(R.id.layout_head);
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setGridLayout(2);
        mPullLoadMoreRecyclerView.setPullRefreshEnable(false);
        subAdapter = new ProductSubAllAdapter(ProductSubjectAllActivity.this);
        mPullLoadMoreRecyclerView.setAdapter(subAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());

    }

    @Override
    protected void initListener() {


    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        /*商店也  专题全部ID*/
        proSubId = intent.getStringExtra("proSubId");
        token = SharePreferencesHelper.getInstance(this).getString("token");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        userId = intent.getStringExtra("userId");
        title = intent.getStringExtra("title");
        type = intent.getStringExtra("type");

        if (EmptyUtils.isNotEmpty(title)) {
            if (title.equals("新品")) {
                designerProduct();
                tvTitle.setText("新品全部");
            } else if (title.equals("折扣")) {
                designerProduct();
                tvTitle.setText("折扣全部");
            } else {
                initPullrefresh2();
            }
        } else {
            initPullrefresh2();
        }


    }


    private void designerProduct() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setType(type);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        CommonInterface creativeProductInterface = new CommonInterface(ProductSubjectAllActivity.this, designerHttpListener);
        creativeProductInterface.getInterface("13010", JSON.toJSONString(bean));
    }

    private void initPullrefresh2() {
        CreativeProductBean bean = new CreativeProductBean();
        bean.setToken(token);
        bean.setProSubId(proSubId);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        CommonInterface creativeProductInterface = new CommonInterface(ProductSubjectAllActivity.this, httpListener2);
        creativeProductInterface.getInterface("13008", JSON.toJSONString(bean));
    }

    HttpListener httpListener2 = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                String title = JsonUtil.getFieldValue(response, "title");
                String subtitle = JsonUtil.getFieldValue(response, "subtitle");
                String desc = JsonUtil.getFieldValue(response, "desc");
                tvTitle.setText(title);
                if (EmptyUtils.isNotEmpty(subtitle) && EmptyUtils.isNotEmpty(desc)) {
                    tv_title.setText(subtitle);
                    tv_desc.setText(desc);
                } else {
                    layout_head.setVisibility(View.GONE);
                }
                CreativeProductRequest bean = JsonUtil.parseJsonToBean(response, CreativeProductRequest.class);
                dataList = bean.getProductList();
                subAdapter.addAllData(dataList);
                getData();


            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
    HttpListener designerHttpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                CreativeProductRequest bean = JsonUtil.parseJsonToBean(response, CreativeProductRequest.class);
                dataList = bean.getProductList();
                subAdapter.addAllData(dataList);
                getData();


            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };

    @Override
    protected void clickEvent(View v) {

    }


    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;

            if (EmptyUtils.isNotEmpty(title)) {
                if (title.equals("新品")) {
                    designerProduct();
                    tvTitle.setText("新品全部");
                } else if (title.equals("折扣")) {
                    designerProduct();
                    tvTitle.setText("折扣全部");
                } else {
                    initPullrefresh2();
                }
            } else {
                initPullrefresh2();
            }
        }
    }

    private void setRefresh() {
        subAdapter.clearData();
        page = 1;
        if (EmptyUtils.isNotEmpty(title)) {
            if (title.equals("新品")) {
                designerProduct();
                tvTitle.setText("新品全部");
            } else if (title.equals("折扣")) {
                designerProduct();
                tvTitle.setText("折扣全部");
            } else {
                initPullrefresh2();
            }
        } else {
            initPullrefresh2();
        }
    }
}
